const CategoryModel = require('../models/CategoryModel')
const TypeModel = require('../models/TypeModel')
const CarModel = require('../models/CarModel')

const categoryService = {
  getCategory() {
    return CategoryModel.find()
  },
  getCarId() {
    return CategoryModel.findOne()
  },
  addCategory(name) {
    return CategoryModel.create({
      name,
    })
  },
  updateCategory({ id, name }) {
    return CategoryModel.updateOne(
      {
        _id: id,
      },
      { $set: { name } }
    )
  },
  async deleteCategory(_id) {
    await TypeModel.deleteMany({
      category_id: _id,
    }).exec()
    await CarModel.deleteMany({
      category_id: _id,
    }).exec()
    return CategoryModel.deleteOne({
      _id,
    })
  },
}

module.exports = categoryService
